home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / NETUSER.C < prev    next >
C/C++ Source or Header  |  1990-05-02  |  7KB  |  349 lines

  1. /* Miscellaneous format conversion subroutines */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "netuser.h"
  7. #include "internet.h"
  8. int net_error;
  9. extern char nospace[];
  10. extern char cantopen[];
  11.  
  12. #define LINELEN 256
  13.  
  14. #ifndef MSDOS
  15. /* define a hostname->ip_address cache, except for MS-DOS */
  16. /* (MS-DOS machines already have enough memspace problems without it!) */
  17. # define NULLHOST (struct host *) 0
  18. static struct host {
  19.     struct host *next;
  20.     int32 ip_address;
  21.     char hostname[2];
  22. } *hostlist = NULLHOST;
  23. #endif
  24.  
  25. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  26.  * binary IP address
  27.  */
  28. int32
  29. aton(s)
  30. register char *s;
  31. {
  32.     int32 n;
  33.     register int i;
  34.  
  35.     n = 0;
  36.     for(i=24;i>=0;i -= 8){
  37.         n |= (int32)atoi(s) << i;
  38.         if((s = index(s,'.')) == NULLCHAR)
  39.             break;
  40.         s++;
  41.     }
  42.     return n;
  43. }
  44. /* Resolve a host name into an IP address. IP addresses in dotted-decimal
  45.  * notation are distinguished from domain names by enclosing them in
  46.  * brackets, e.g., [44.64.0.1]
  47.  */
  48. int32
  49. resolve(host)
  50. char *host;
  51. {
  52. #ifdef MSDOS
  53.     static struct {
  54.         char *name;
  55.         int32 address;
  56.     } cache;
  57. #else
  58.     register struct host *hp;
  59.     register struct host **hpp;
  60.     int32 ip_address;
  61. #endif
  62.     register char *cp,*cp1;
  63.     char *getnenv();
  64.     FILE *sfile;
  65.     int  len,l;
  66.     char hostent[LINELEN];
  67.  
  68.     if(*host == '['){
  69.         /* Brackets indicate IP address in dotted-decimal form */
  70.         return aton(host + 1);
  71.     }
  72. #ifdef MSDOS
  73.     if(cache.name != NULLCHAR && strcmp(cache.name,host) == 0)
  74.         return cache.address;
  75.  
  76.     len = strlen(host);
  77. #else
  78.     len = strlen(host);
  79.  
  80.     /* Not a numerical IP address, look in in-memory host list */
  81.     for(hpp = &hostlist; *hpp != NULLHOST; hpp = &hp->next){
  82.         hp = *hpp;
  83.         if(strlen(hp->hostname) == len &&
  84.            strncasecmp(host,hp->hostname,len) == 0)
  85.         return hp->ip_address;
  86.     }
  87. #endif
  88.     /* Not a numerical IP address, search the host table */
  89.     if((sfile = fopen(cp = getnenv(HOSTS),"r")) == NULLFILE){
  90.         printf(cantopen,cp);
  91.         return 0;
  92.     }
  93.     while (fgets(hostent,LINELEN,sfile) != NULLCHAR){
  94.         rip(hostent);
  95.         cp = hostent;
  96.         if(*cp == '#' || !isdigit(*cp))
  97.             continue;    /* Comment or invalid line */
  98. #ifndef MSDOS
  99.         ip_address = aton(cp);
  100.         /* skip to end of IP address */
  101.         while(*cp != '\0' && *cp != ' ' && *cp != '\t')
  102.             cp++;
  103. #endif
  104.         while(*cp){
  105.             /* Skip white space */
  106.             while(*cp == ' ' || *cp == '\t')
  107.                 cp++;
  108.             if(*cp == '\0' || *cp == '#')
  109.                 break;
  110.             /* Look for next token, find length of this one */
  111.             for (cp1 = cp; *cp1 && *cp1 != ' ' && *cp1 != '\t'; cp1++)
  112.                 ;
  113.             l = (int) (cp1 - cp);
  114.             if(len == l && strncasecmp(host,cp,l) == 0){
  115.                 /* Found it, put in cache */
  116.                 fclose(sfile);
  117. #ifdef MSDOS
  118.                 if(cache.name != NULLCHAR)
  119.                     free(cache.name);
  120.                 if((cache.name = malloc((unsigned)l+1)) != NULLCHAR)
  121.                     strcpy(cache.name,host);
  122.                 cache.address = aton(hostent);
  123.                 return cache.address;
  124. #else
  125.                 if ((hp = (struct host *) calloc(1,sizeof(struct host) + len - 1)) == NULLHOST){
  126.                     printf(nospace);
  127.                 } else {
  128.                     /* fill new host block */
  129.                     *hpp = hp;
  130.                     hp->ip_address = ip_address;
  131.                     strncpy(hp->hostname,cp,len);
  132.                 }
  133.                 return ip_address;
  134. #endif
  135.             }
  136.             /* That one didn't match, try the next one */
  137.             cp = cp1;
  138.         }
  139.     }
  140.     /* No address found */
  141.     fclose(sfile);
  142.     return 0;
  143. }
  144.  
  145. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  146.  * string, e.g., 255.255.255.255\0
  147.  */
  148. char *
  149. inet_ntoa(a)
  150. int32 a;
  151. {
  152.     static char buf[16];
  153.  
  154.     sprintf(buf,"%u.%u.%u.%u",
  155.         hibyte(hiword(a)),
  156.         lobyte(hiword(a)),
  157.         hibyte(loword(a)),
  158.         lobyte(loword(a)) );
  159.     return buf;
  160. }
  161. /* Convert a socket (address + port) to an ascii string of the form
  162.  * aaa.aaa.aaa.aaa:ppppp
  163.  */
  164. char *
  165. psocket(s)
  166. struct socket *s;
  167. {
  168.     static char buf[22];
  169.  
  170.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  171.     return buf;
  172. }
  173. /* Convert an internet address (in host byte order) to first host name
  174.  * from hosts file (if present) or else to dotted decimal
  175.  */
  176. char *
  177. inet_n2h(a)
  178. int32 a;
  179. {
  180. #ifdef MSDOS
  181.     static struct {
  182.         char *name;
  183.         int32 address;
  184.     } cache;
  185. #else
  186.     register struct host *hp;
  187.     register struct host **hpp;
  188. #endif
  189.     register char *cp,*cp1;
  190.     char *getnenv();
  191.     FILE *sfile;
  192.     char hostent[LINELEN];
  193.  
  194.     if(a == 0)
  195.         return inet_ntoa(a);
  196.     /* First look in the cache */
  197. #ifdef MSDOS
  198.     if(cache.name != NULLCHAR && cache.address == a)
  199.         return cache.name;
  200. #else
  201.     for(hpp = &hostlist; *hpp != NULLHOST; hpp = &hp->next){
  202.         hp = *hpp;
  203.         if(a == hp->ip_address)
  204.         return hp->hostname;
  205.     }
  206. #endif
  207.  
  208.     /* Not in the cache, search the host table */
  209.     if((sfile = fopen(getnenv(HOSTS),"r")) == NULLFILE)
  210.         return inet_ntoa(a);
  211.     while (fgets(hostent,LINELEN,sfile) != NULLCHAR){
  212.         rip(hostent);
  213.         cp = hostent;
  214.         if(*cp == '#' || !isdigit(*cp))
  215.             continue;    /* Comment or invalid line */
  216.         if(a == aton(cp)) {
  217.             /* Found it, put in cache */
  218.             fclose(sfile);
  219.             /* Look for hostname, first skip numerical form */
  220.             while(*cp != ' ' && *cp != '\t' && *cp)
  221.                 cp++;
  222.             /* Skip white space */
  223.             while(*cp == ' ' || *cp == '\t')
  224.                 cp++;
  225.             if(*cp == '\0' || *cp == '#')
  226.                 return inet_ntoa(a);
  227.             /* Look for end of token */
  228.             for (cp1 = cp; *cp1 && *cp1 != ' ' && *cp1 != '\t'; cp1++)
  229.                 ;
  230.             /* Terminate the host name */
  231.             *cp1 = '\0';
  232. #ifdef MSDOS
  233.             if(cache.name != NULLCHAR)
  234.                 free(cache.name);
  235.             if((cache.name = malloc((unsigned)(strlen(cp)+1))) == NULLCHAR)
  236.                 return inet_ntoa(a);
  237.             strcpy(cache.name,cp);
  238.             cache.address = a;
  239.             return cache.name;
  240. #else
  241.             if ((hp = (struct host *) calloc(1,sizeof(struct host)+strlen(cp)-1)) == NULLHOST){
  242.                 return inet_ntoa(a);
  243.             }
  244.             /* fill new host block */
  245.             *hpp = hp;
  246.             hp->ip_address = a;
  247.             strcpy(hp->hostname,cp);
  248.             return hp->hostname;
  249. #endif
  250.         }
  251.     }
  252.     /* No address found */
  253.     fclose(sfile);
  254.     return inet_ntoa(a);
  255. }
  256. /* Convert a socket (address + port) to an ascii string of the form
  257.  * hostname:ppppp
  258.  */
  259. char *
  260. phsocket(s)
  261. struct socket *s;
  262. {
  263.     static char buf[80];
  264.  
  265.     sprintf(buf,"%s:%u",inet_n2h(s->address),s->port);
  266.     return buf;
  267. }
  268. /* Convert hex-ascii string to long integer */
  269. long
  270. htol(s)
  271. char *s;
  272. {
  273.     long ret;
  274.     char c;
  275.  
  276.     ret = 0;
  277.     while((c = *s++) != '\0'){
  278.         c &= 0x7f;
  279.         if(c >= '0' && c <= '9')
  280.             ret = ret*16 + (c - '0');
  281.         else if(c >= 'a' && c <= 'f')
  282.             ret = ret*16 + (10 + c - 'a');
  283.         else if(c >= 'A' && c <= 'F')
  284.             ret = ret*16 + (10 + c - 'A');
  285.         else
  286.             break;
  287.     }
  288.     return ret;
  289. }
  290. /* convert TOS specification to binary value */
  291. int
  292. get_tos (tos_spec)
  293. char *tos_spec;
  294.  
  295. {
  296.     int tos = 0;
  297.     int c;
  298.  
  299.     if(tos_spec != NULLCHAR) {
  300.         while ((c = *tos_spec++) != '\0') {
  301.         switch (toupper(c))
  302.         {
  303.         case '0':
  304.         case '1':
  305.         case '2':
  306.         case '3':
  307.         case '4':
  308.         case '5':
  309.         case '6':
  310.         case '7':
  311.             tos |= (c & 7) << 5;
  312.             break;
  313.  
  314.         case 'D':
  315.             tos |= LOW_DELAY;
  316.             break;
  317.  
  318.         case 'T':
  319.             tos |= THROUGHPUT;
  320.             break;
  321.  
  322.         case 'R':
  323.             tos |= RELIABILITY;
  324.             break;
  325.         }
  326.         }
  327.     }
  328.  
  329.     return tos;
  330. }
  331. /* convert TOS to printable string */
  332. char *
  333. put_tos (tos)
  334. int tos;
  335. {
  336.     static char result[5];
  337.     register char *rp = result;
  338.  
  339.     *rp++ = PREC(tos) + '0';
  340.     if (tos & LOW_DELAY)
  341.         *rp++ = 'D';
  342.     if (tos & THROUGHPUT)
  343.         *rp++ = 'T';
  344.     if (tos & RELIABILITY)
  345.         *rp++ = 'R';
  346.     *rp = '\0';
  347.     return result;
  348. }
  349.